class aiEntity{
	// unit count variables
	private var lightUnits:Number;
	private var mediumUnits:Number;
	private var heavyUnits:Number;
	private var buffBuildings:Number;
	private var debuffBuildings:Number;
	
	private var myManager:manager; //the manager controlling this aiEntity
	private var parent:player;	//the player controlled by this aiEntity
	
	/**
	 * Default constructor	 */
	public function aiEntity(){
		lightUnits = 0;
		mediumUnits = 0;
		heavyUnits = 0;
		buffBuildings = 0;
		debuffBuildings = 0;
	}
	
	/**
	 * Sets the parent to the specified value
	 * @param p the value to set parent to	 */
	public function setParent(p:player):Void{
		parent = p;
	}
	
	/**
	 * Returns the parent for this 
	 * @return the parent for this	 */
	public function getParent():player{
		return parent;
	}
	
	/**
	 * Sets the value of myManager to the specified value
	 * @param m the value to set myManager to	 */
	public function setManager(m:manager):Void{
		myManager = m;
	}
	
	/**
	 * Updates the current state of this aiEntity	 */
	public function update():Void{
		if(parent.canCast())
			playCard();
	}
	
	/**
	 * Chooses and plays card if possible	 */
	private function playCard():Void{
		var cards:Array = parent.getHand();

		var tmpCard:card;
		
		//find a card to use
		tmpCard = getCardOfType("mediumunit");
		if(tmpCard == null)
			tmpCard = getCardOfType("buffbuilding");
		if(tmpCard == null)
			tmpCard = getCardOfType("heavyunit");
			
		if(tmpCard != null){
			myManager.useCard(parent, tmpCard);
			return;
		}
			
			

		//loop through all the cards and select one to play
		//TODO for now pick Angel if available
		/*for(var i = 0; i < cards.length; i++){
			if(cards[i].getType() == "mediumunit" && parent.canCast()){
				myManager.useCard(parent, cards[i]);	
				return;
			}
			else if(cards[i].getType() == "heavyunit" && parent.canCast()){
				myManager.useCard(parent, cards[i]);	
				return;
			}
			else if(cards[i].getType() == "buffbuilding" && parent.canCast()){
				myManager.useCard(parent, cards[i]);	
				break;
			}
			
		}*/
		
	}
	
	/**
	 * Retrieves the first instance of the type specified, or null if none are found
	 * @param type the type of card we're looking for
	 * @return a card that matches the type, or null if none found	 */
	private function getCardOfType(type:String):card{
		var hand:Array = parent.getHand();
		for(var i = 0; i < config.handSize; i++)
			if(hand[i].getType() == type)
				return hand[i];
				
		return null; //no matches found
	}
}